home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 959 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  80 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: jbuck@Synopsys.COM (Joe Buck)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: strings as stl container
  5. Date: 04 Apr 1996 10:15:18 PST
  6. Organization: Synopsys Inc., Mountain View, CA 94043-4033
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4jvdnd$c9c@hermes.synopsys.com>
  9. References: <199604020907.LAA06972@bredex.bredex.de>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 4 Apr 1996 02:55:41 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMWQRt0y4NqrwXLNJAQFVCwIAli45uYsIJxE/R7zrjj+vOhMBEpH7hbp2
  14.     bib37fRT6wJV7ocVQ0EEsjKlfEhnbD/9uizBlUIGUE/a9VVIezCToQ==
  15.     =lh7T
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. Nico Josuttis <nico@bredex.de> writes:
  19. >Due to the fact that strings have iterator support,
  20. >the could be used as stl container.
  21.  
  22. Correct.
  23.  
  24. >But two things are missing:
  25. > - push_back()
  26. > - clear()
  27.  
  28. No, these are not needed.
  29.  
  30. >These are fundamental operations, that the corresponding "normal"
  31. >container, namely vector, has.
  32.  
  33. The documentation I have does not list clear as a member of vector.
  34.  
  35. >Especially push_back() would be essential to use insert iterators
  36. >for strings.
  37.  
  38. No, this isn't true.  You need push_back to use back_insert_iterator,
  39. but insert_iterator only needs insert, which string does have.
  40.  
  41. Try the following program (works with gcc 2.7.2/libg++ 2.7.1 and appears
  42. to conform to the standard if I didn't screw up):
  43.  
  44. ----------------------
  45. #include <string>
  46. #include <iterator>
  47. #include <algorithm>
  48.  
  49. const char text[] = "This is text\n";
  50. const char text2[] = "even more ";
  51.  
  52. int main()
  53. {
  54.     string foo;
  55.     insert_iterator<string> iter = inserter(foo, foo.begin());
  56.     copy(text, text + sizeof(text)-1, iter);
  57.     cout << "value of foo: " << foo;
  58.  
  59.     copy(text2, text2 + sizeof(text2)-1, inserter(foo, foo.begin()+8));
  60.     cout << "value of foo: " << foo;
  61. }
  62. ----------------------
  63.      
  64. This prints
  65. value of foo: This is text
  66. value of foo: This is even more text
  67.  
  68. -- 
  69. -- Joe Buck     <jbuck@synopsys.com>    (not speaking for Synopsys, Inc)
  70.  
  71. Work for something because it is good,
  72. not just because it stands a chance to succeed.       -- Vaclav Havel
  73. ---
  74. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  75.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  76.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  77.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  78.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  79. ]
  80.